package org.example.elasticsearch.config;

import co.elastic.clients.elasticsearch.ElasticsearchClient;
import co.elastic.clients.transport.ElasticsearchTransport;
import org.example.elasticsearch.ESService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.elasticsearch.ElasticsearchProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author: sql
 * @date: 2024/6/7
 * @Description: ES 配置
 * @version: 1.0
 */
@Slf4j
@Configuration
@EnableConfigurationProperties({ElasticsearchProperties.class})
@ConditionalOnClass({ElasticsearchClient.class, ElasticsearchTransport.class})
public class ESRestClientConfiguration {

    private final ElasticsearchProperties elasticsearchProperties;

    public ESRestClientConfiguration(ElasticsearchProperties elasticsearchProperties) {
        log.info("elasticsearch elasticsearchProperties loading");
        this.elasticsearchProperties = elasticsearchProperties;
    }

    @Bean
    public ElasticsearchClient elasticsearchClient() {
        log.info("elasticsearch elasticsearchClient loading");
        return ESRestClientBuilder.createClient(elasticsearchProperties);
    }

    @Bean
    public ESService elasticsearchService(ElasticsearchClient elasticsearchClient) {
        return new ESService(elasticsearchClient, elasticsearchProperties);
    }

}

